import 'dart:async';

import 'package:flutter/material.dart';

class ComSkeleton extends StatefulWidget {
  final Widget child;
  final double width;
  final double height;
  final bool isLoading;

  const ComSkeleton({
    super.key,
    required this.child,
    required this.width,
    required this.height,
    required this.isLoading,
  });

  @override
  State<ComSkeleton> createState() => _ComSkeletonState();
}

class _ComSkeletonState extends State<ComSkeleton>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late ColorTween colorTween;
  late Animation<Color?> animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 1),
      reverseDuration: Duration(seconds: 1),
    );
    colorTween = ColorTween(
      begin: Color.fromRGBO(0, 0, 0, 0.1),
      end: Color.fromRGBO(0, 0, 0, 0.2),
    );
    animation = colorTween.animate(_controller);
    _controller.repeat(reverse: true);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return widget.isLoading
        ? AnimatedBuilder(
          animation: animation,
          builder: (ctx, child) {
            return Container(
              color: animation.value,
              width: widget.width,
              height: widget.height,
            );
          },
        )
        : Container(child: widget.child);
  }
}
